<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="profileObj.photo"
            @click="imgClickFn"
          />
          <!-- file 选择框 -->
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="changeFileFn"
          />
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="profileObj.name" />
      <!-- <van-cell title="性别" is-link :value="profileObj.gender === 1 ? '女' : profileObj.gender === 0 ? '男' : '保密'"/> -->
      <van-cell title="性别" is-link :value="sexObj[profileObj.gender]" />
      <van-cell title="生日" is-link :value="profileObj.birthday" />
    </van-cell-group>
  </div>
</template>

<script>
import { getUserProfileAPI, uploadHeadImgAPI } from '@/api'
export default {
  data () {
    return {
      profileObj: {},
      sexObj: {
        1: '女',
        0: '男',
        '-1': '保密'
      }
    }
  },
  name: 'UserEdit',
  async created () {
    const res = await getUserProfileAPI()
    console.log(res)
    this.profileObj = res.data.data
  },
  methods: {
    imgClickFn () {
      this.$refs.iptFile.click()
    },
    async changeFileFn (ev) {
      console.log(ev.target.files[0])
      const formData = new FormData()
      formData.append('photo', ev.target.files[0])
      const res = await uploadHeadImgAPI(formData)
      console.log(res)
      console.log('图片上传')
      this.profileObj.photo = res.data.data.photo
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
